package com.kyxx.topics.fragment;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kyxx.topics.BaseApp;
import com.kyxx.topics.R;
import com.kyxx.topics.activity.AboutActivity;
import com.kyxx.topics.activity.FavoriteActivity;
import com.kyxx.topics.activity.FeedBackActivity2;
import com.kyxx.topics.activity.HistoryActivity;
import com.kyxx.topics.activity.MessageActivity;
import com.kyxx.topics.activity.SettingActivity;
import com.kyxx.topics.activity.TipOffActivity;
import com.kyxx.topics.activity.UserInfoActivity;
import com.kyxx.topics.activity.UserLoginActivity2;
import com.kyxx.topics.utils.CommonUtils;
import com.kyxx.topics.utils.StringUtil;
import com.kyxx.topics.utils.ToastUtils;
import com.kyxx.topics.view.CircleImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

public class MineFragment extends Fragment implements View.OnClickListener {

    private CircleImageView imgAvatar;
    private TextView tvUserName, tvUserLogin, tvPayOfWater, tvPayOfNet;
    private LinearLayout layCollection, layFocus, laySetting, layPayOfProperty, layPayOfWater, layPayOfElect, layPayOfGas, layPayOfTv, layPayOfNet, layService, layCOmlaint, layCommunityBBS;
    private RelativeLayout rl_tip_off,rl_feed_back;
    private View view;

    private RelativeLayout rl_message;
    private RelativeLayout rl_6;
    private RelativeLayout rl_2;
    private RelativeLayout rl_4;
    private DisplayImageOptions options = new DisplayImageOptions.Builder()
//				// 设置图片在下载期间显示的图片
//            .showImageOnLoading(R.mipmap.icon_user_default)
//				// 设置图片Uri为空或是错误的时候显示的图片
    .showImageForEmptyUri(R.mipmap.icon_user_default)
//				// 设置图片加载/解码过程中错误时候显示的图片
    .showImageOnFail(R.mipmap.icon_user_default)
    .cacheInMemory(true)
    // 设置下载的图片是否缓存在内存中
    .cacheOnDisk(true)
    // 设置下载的图片是否缓存在SD卡中
    .resetViewBeforeLoading(false)
    .bitmapConfig(Bitmap.Config.RGB_565)// 设置图片的解码类型
//                .displayer(new RoundedBitmapDisplayer(9999))
    .build();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_mine2, container, false);
        initView();
        initListener();
        return view;
    }

    /**
     * 初始化控件
     */
    private void initView() {
        imgAvatar = (CircleImageView) view.findViewById(R.id.riv_user_avatar);//头像
        tvUserName = (TextView) view.findViewById(R.id.tv_user_name);//昵称
        tvUserLogin = (TextView) view.findViewById(R.id.tv_user_login);//登录
        tvPayOfWater = (TextView) view.findViewById(R.id.tv_pay_water);//可用水费
        tvPayOfNet = (TextView) view.findViewById(R.id.tv_pay_net);//可用网费

        layCollection = (LinearLayout) view.findViewById(R.id.ll_collection);//收藏
        layFocus = (LinearLayout) view.findViewById(R.id.ll_focus);//关注
        laySetting = (LinearLayout) view.findViewById(R.id.ll_setting);//设置
        rl_tip_off = (RelativeLayout) view.findViewById(R.id.rl_tip_off);
        rl_feed_back = (RelativeLayout) view.findViewById(R.id.rl_feed_back);
//        layPayOfProperty = (LinearLayout) view.findViewById(R.id.ll_payment_property);//物业缴费
//        layPayOfWater = (LinearLayout) view.findViewById(R.id.ll_payment_water);//水费缴纳
//        layPayOfElect = (LinearLayout) view.findViewById(R.id.ll_payment_elect);//电费缴纳
//        layPayOfGas = (LinearLayout) view.findViewById(R.id.ll_payment_gas);//燃气缴费
//        layPayOfTv = (LinearLayout) view.findViewById(R.id.ll_payment_tv);//电视缴费
//        layPayOfNet = (LinearLayout) view.findViewById(R.id.ll_payment_net);//宽带缴费
//        layService = (LinearLayout) view.findViewById(R.id.ll_service);//报障维修
//        layCOmlaint = (LinearLayout) view.findViewById(R.id.ll_complaint);//投诉建议
//        layCommunityBBS = (LinearLayout) view.findViewById(R.id.ll_community_bbs);//社区论坛

        rl_message = (RelativeLayout) view.findViewById(R.id.rl_message);
        rl_6 = (RelativeLayout) view.findViewById(R.id.rl_6);
        rl_2 = (RelativeLayout) view.findViewById(R.id.rl_2);
        rl_4 = (RelativeLayout) view.findViewById(R.id.rl_4);

    }

    /**
     * 绑定监听事件
     */
    private void initListener() {
        imgAvatar.setOnClickListener(this);
        layCollection.setOnClickListener(this);
        layFocus.setOnClickListener(this);
        laySetting.setOnClickListener(this);
        rl_message.setOnClickListener(this);
        rl_6.setOnClickListener(this);
        rl_2.setOnClickListener(this);
        rl_4.setOnClickListener(this);
        rl_tip_off.setOnClickListener(this);
        rl_feed_back.setOnClickListener(this);
//        layPayOfProperty.setOnClickListener(this);
//        layPayOfWater.setOnClickListener(this);
//        layPayOfElect.setOnClickListener(this);
//        layPayOfGas.setOnClickListener(this);
//        layPayOfTv.setOnClickListener(this);
//        layPayOfNet.setOnClickListener(this);
//        layService.setOnClickListener(this);
//        layCOmlaint.setOnClickListener(this);
//        layCommunityBBS.setOnClickListener(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        requestData();
    }

    /**
     * 请求数据,赋值
     */
    private void requestData() {
        /**
         * 登录成功显示昵称,未登录显示登录
         */
        if (StringUtil.isEmpty(BaseApp.getModel().getId())) {
            tvUserLogin.setVisibility(View.VISIBLE);
            tvUserName.setVisibility(View.GONE);
        } else {
            tvUserLogin.setVisibility(View.GONE);
            tvUserName.setVisibility(View.VISIBLE);
        }
        String aa = BaseApp.getModel().getNickname();
        tvUserName.setText(BaseApp.getModel().getNickname());
        ImageLoader.getInstance().displayImage(BaseApp.getModel().getAvatar(), imgAvatar, options);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.riv_user_avatar://头像,登录后跳转个人中心,未登录跳转登录界面
                if (StringUtil.isEmpty(BaseApp.getModel().getId())) {
                    startActivity(new Intent(getActivity(), UserLoginActivity2.class));
                } else {
                    startActivity(new Intent(getActivity(), UserInfoActivity.class));
                }
                break;
            case R.id.ll_collection://收藏
                if (TextUtils.isEmpty(BaseApp.getModel().getId())) {
                    startActivity(new Intent(getActivity(), UserLoginActivity2.class));
                    return;
                }
                startActivity(new Intent(getActivity(), FavoriteActivity.class));
                break;
            case R.id.ll_focus://关注
                if (TextUtils.isEmpty(BaseApp.getModel().getId())) {
                    startActivity(new Intent(getActivity(), UserLoginActivity2.class));
                    return;
                }

                break;
            case R.id.ll_setting://设置
//                if (TextUtils.isEmpty(BaseApp.getModel().getId())) {
//                    startActivity(new Intent(getActivity(), UserLoginActivity2.class));
//                    return;
//                }
                startActivity(new Intent(getActivity(), SettingActivity.class));
                break;
            case R.id.rl_tip_off://设置
                if (TextUtils.isEmpty(BaseApp.getModel().getId())) {
                    startActivity(new Intent(getActivity(), UserLoginActivity2.class));
                    return;
                }
                startActivity(new Intent(getActivity(), TipOffActivity .class));
                break;

            case R.id.rl_message:
                if (TextUtils.isEmpty(BaseApp.getModel().getId())) {
                    startActivity(new Intent(getActivity(), UserLoginActivity2.class));
                    return;
                }
                startActivity(new Intent(getActivity(), MessageActivity.class));
                break;
            case R.id.rl_6:
                startActivity(new Intent(getActivity(), AboutActivity.class));
                break;
            case R.id.rl_2:
                if (TextUtils.isEmpty(BaseApp.getModel().getId())) {
                    startActivity(new Intent(getActivity(), UserLoginActivity2.class));
                    return;
                }
                startActivity(new Intent(getActivity(), HistoryActivity.class));
                break;
            case R.id.rl_4:
                CommonUtils.showShare(getActivity(), "沂蒙", " ", "http://www.acfun.tv/v/list164/index_3.htm");
                break;
            case R.id.rl_feed_back://意见反馈
                if (TextUtils.isEmpty(BaseApp.getModel().getId())) {
                    startActivity(new Intent(getActivity(), UserLoginActivity2.class));
                    return;
                }
                startActivity(new Intent(getActivity(),FeedBackActivity2.class));
                break;
//            case R.id.ll_payment_property://物业缴费
//
//                break;
//            case R.id.ll_payment_water://水费缴纳
//
//                break;
//            case R.id.ll_payment_elect://电费缴纳
//
//                break;
//            case R.id.ll_payment_gas://燃气缴费
//
//                break;
//            case R.id.ll_payment_tv://电视缴费
//
//                break;
//            case R.id.ll_payment_net://宽带缴费
//
//                break;
//            case R.id.ll_service://报障维修
//
//                break;
//            case R.id.ll_complaint://投诉建议
//
//                break;
//            case R.id.ll_community_bbs://社区论坛
//
//                break;
            default:
                break;
        }
    }

    private void goShare0() {
        List<Intent> targetedShareIntents = new ArrayList<Intent>();
        targetedShareIntents.add(initPlatform("主题", "这是我的分享内容", "com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI"));
        targetedShareIntents.add(initPlatform("主题", "这是我的分享内容", "com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI"));
        targetedShareIntents.add(initPlatform("主题", "这是我的分享内容", "com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity"));
        targetedShareIntents.add(initPlatform("主题", "这是我的分享内容", "com.sina.weibo", "com.tencent.mm.ui.tools.ShareImgUI"));
        // 选择分享时的标题
        Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "选择分享");
        if (chooserIntent == null) {
            return;
        }
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
        try {
            startActivity(chooserIntent);
        } catch (android.content.ActivityNotFoundException ex) {
            ToastUtils.show(getActivity(), "找不到该分享应用组件");
        }
    }

    private Intent initPlatform(String title, String content, String packageName, String className) {
        Intent targeted = new Intent(Intent.ACTION_SEND);
        targeted.setType("text/plain");
        // 分享出去的内容
        targeted.putExtra(Intent.EXTRA_TEXT, content + getActivity().getPackageName());
        // 分享出去的标题
        targeted.putExtra(Intent.EXTRA_SUBJECT, title);
        targeted.setPackage(packageName);
        targeted.setClassName(packageName, className);
        PackageManager pm = getActivity().getApplication().getPackageManager();
        return targeted;
    }
}
